import { apiClient } from './index'
import { mockApi } from '@/utils/mockApi'
import type { 
  TaskRun,
  ApiResponse,
  PaginationResponse,
  QueryParams 
} from '@/types'

export const taskApi = {
  // 获取任务列表
  getTasks: async (params?: QueryParams): Promise<ApiResponse<PaginationResponse<TaskRun>>> => {
    // 模拟数据
    const mockTasks: TaskRun[] = [
      {
        id: 1,
        query_id: 'query_001',
        task_name: '数据同步任务',
        state: 'RUNNING',
        create_time: '2024-01-15T10:00:00Z',
        finish_time: undefined,
        catalog: 'starrocks',
        database_name: 'test_db',
        definition: 'SELECT * FROM test_table',
        progress: '75',
        created_at: '2024-01-15T10:00:00Z'
      },
      {
        id: 2,
        query_id: 'query_002',
        task_name: '报表生成任务',
        state: 'FINISHED',
        create_time: '2024-01-15T09:00:00Z',
        finish_time: '2024-01-15T09:30:00Z',
        catalog: 'starrocks',
        database_name: 'test_db',
        definition: 'SELECT COUNT(*) FROM test_table',
        progress: '100',
        created_at: '2024-01-15T09:00:00Z'
      },
      {
        id: 3,
        query_id: 'query_003',
        task_name: '数据清洗任务',
        state: 'FAILED',
        create_time: '2024-01-15T08:00:00Z',
        finish_time: '2024-01-15T08:15:00Z',
        catalog: 'starrocks',
        database_name: 'test_db',
        definition: 'INSERT INTO clean_table SELECT * FROM raw_table WHERE status = "valid"',
        progress: '45',
        created_at: '2024-01-15T08:00:00Z'
      },
      {
        id: 4,
        query_id: 'query_004',
        task_name: '数据聚合任务',
        state: 'CANCELED',
        create_time: '2024-01-15T07:00:00Z',
        finish_time: undefined,
        catalog: 'starrocks',
        database_name: 'test_db',
        definition: 'SELECT date, SUM(amount) FROM transactions GROUP BY date',
        progress: '30',
        created_at: '2024-01-15T07:00:00Z'
      }
    ]
    
    return {
      code: 200,
      message: 'success',
      data: {
        items: mockTasks,
        pagination: {
          page: 1,
          size: 20,
          total: mockTasks.length,
          pages: 1
        }
      },
      timestamp: new Date().toISOString()
    }
  },

  // 获取任务详情
  getTaskDetail: async (taskId: number): Promise<ApiResponse<TaskRun>> => {
    return {
      code: 200,
      message: 'success',
      data: {} as TaskRun,
      timestamp: new Date().toISOString()
    }
  },

  // 取消任务
  cancelTask: async (taskId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 重试任务
  retryTask: async (taskId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 获取任务告警
  getTaskAlerts: async (taskId: number): Promise<ApiResponse<any[]>> => {
    return {
      code: 200,
      message: 'success',
      data: [],
      timestamp: new Date().toISOString()
    }
  },

  // 批量重试任务
  batchRetryTasks: async (taskIds: number[]): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: { retried_count: taskIds.length },
      timestamp: new Date().toISOString()
    }
  },

  // 批量取消任务
  batchCancelTasks: async (taskIds: number[]): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: { canceled_count: taskIds.length },
      timestamp: new Date().toISOString()
    }
  }
} 